package com.gykfbxxh.imovie.view.user.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.utils.util.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.base.BaseActivity;
import com.gykfbxxh.imovie.bean.info.UserInfor;
import com.gykfbxxh.imovie.bean.info.UserInforBean;
import com.gykfbxxh.imovie.config.ConfigKey;
import com.gykfbxxh.imovie.config.URIContant;
import com.gykfbxxh.imovie.custom.net.ParamMap;
import com.gykfbxxh.imovie.listener.HttpListener;
import com.gykfbxxh.imovie.manager.AppConfigManager;
import com.gykfbxxh.imovie.manager.UserInforManager;
import com.gykfbxxh.imovie.utils.HttpUtils;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_login,tv_button,tv_wangjipwd,tv_register;
    private EditText et_phonenum,et_password;
    private ImageView iv_button;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addContentView(R.layout.activity_login);
        setTitle("登录");
        mPageName = "LoginActivity" ;
        initView();

//        tv_login = (TextView)findViewById(R.id.tv_login);
//        tv_login.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                loginData();
//            }
//        });
    }

    private void initView() {
        et_phonenum = (EditText)findViewById(R.id.et_phonenum);
        et_password = (EditText)findViewById(R.id.et_password);
        et_phonenum.addTextChangedListener(tw);
        et_password.addTextChangedListener(tw);
        tv_button = (TextView)findViewById(R.id.tv_button);
        iv_button = (ImageView)findViewById(R.id.iv_button);
        tv_wangjipwd = (TextView)findViewById(R.id.tv_wangjipwd);
        tv_register = (TextView)findViewById(R.id.tv_register);

        tv_button.setOnClickListener(this);
        tv_wangjipwd.setOnClickListener(this);
        tv_register.setOnClickListener(this);
    }


    /**
     * edittext的监听
     */
    TextWatcher tw = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(et_phonenum == null || et_password == null || tv_button == null ||iv_button ==null) return;
            if(et_password.getText().toString().trim().length()>=6
                    && et_phonenum.getText().toString().trim().length() >= 8) {
                tv_button.setEnabled(true);
                iv_button.setImageResource(R.drawable.confirm_2);
            }else{
                tv_button.setEnabled(false);
                iv_button.setImageResource(R.drawable.confirm);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    private void loginData() {
        if (et_password == null || et_phonenum == null) {
            ToastUtils.showText("程序异常,请重新登录");
            return;
        }
        String phoneNum = et_phonenum.getText().toString();
        if (TextUtils.isEmpty(phoneNum)) {
            ToastUtils.makeText("请输入帐号");
            return;
        }
        final String pwd = et_password.getText().toString();
        if (TextUtils.isEmpty(pwd)) {
            ToastUtils.makeText("请输入密码");
            return;
        }

        if(request_state) return;

        ParamMap paramMap = new ParamMap();
        paramMap.put("phone",phoneNum);
        paramMap.put("password",pwd);
        paramMap.put("loginapp","4");
        HttpUtils.ParmsMapMD5(paramMap);
        HttpUtils.addPostRequest(URIContant.LOGINUSER, new TypeToken<UserInforBean>() {
        }.getType(), mPageName, paramMap, new HttpListener<UserInforBean>() {
            @Override
            public void onStart() {
                request_state = true ;
            }

            @Override
            public void onError(VolleyError error) {
                ToastUtils.makeText("登录失败");
                request_state = false;
            }

            @Override
            public void onSuccess(UserInforBean response) {
                if(response!=null&&"LoginSucess".equals(response.getCode())&&response.getResult()!=null) {
                    request_state = true;
                    UserInfor userInfor = response.getResult();
                    //设置用户信息
                    UserInforManager.getInstance().setUserInfor(userInfor);//设置用户信息
                    //保存到本地
                    UserInforManager.getInstance().saveUserInfor(userInfor);
                    SharedPreferences.Editor editor = AppConfigManager.getInstance().getEditor();
                    editor.putString(ConfigKey.USERPWD,pwd);
                    editor.commit();
                    ToastUtils.makeText("登录成功");
                    onBackPressed();
                }else {
                    ToastUtils.makeText(response.getErrorMsg());
                    request_state = false;
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }

    @Override
    public void doBack(View view) {
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        super.doBack(view);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.tv_button:
                loginData();
                break;
            case R.id.tv_wangjipwd:
                Intent intent = new Intent(this, UpdatePwdActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_register:
                Intent intent1 = new Intent(this, RegisterActivity.class);
                startActivity(intent1);
                break;

        }
    }
}
